import {findChatRoomInfoById} from "../../api/chatRoom"
import {getStaffInfo} from "../../api/wenzhen"
import Dialog from '@vant/weapp/dialog/dialog';
import {deleteChatRoomId} from "../../api/userInfo"

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    chatRoomId:String
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    toDetail(){
      let {chatRoomId} = this.data
      wx.navigateTo({
        url: '/pages/menu/xiaoxi/detail/index?chatRoomId='+chatRoomId,
      })
    },
    delete(){
      let {chatRoomId,userId} = this.data
      Dialog.confirm({
        title: '删除聊天记录',
        message: '是否确认删除该聊天记录？',
      })
        .then(async () => {
          // on confirm
           let ret = await deleteChatRoomId(userId,chatRoomId)
           Toast(ret)
           this.triggerEvent('customevent',{})
        })
        .catch(() => {

        });
    },
  },
  lifetimes:{
    async attached(){
      let chatRoomId = this.properties.chatRoomId

      let ret = await findChatRoomInfoById(chatRoomId)
      let {staffId,userId,content} = ret
      let newMsg = content.at(-1).value
      let {imageUrl:staffImgUrl,name} = await getStaffInfo(staffId)
      let firstName = name[0]
      this.setData({
        chatRoomId,
        staffImgUrl,
        newMsg,
        firstName,
        userId
      })
    }
  }
})
